package server;

import net.sf.json.JSONObject;
import util.Encode;
import util.Hash;
import util.RandomPassword;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * UserRank: tum0r
 * Package Name: server
 * Time: 2019-01-21 Monday 15:53:24
 */
public class InitServer extends BaseServer {
    @Override
    public boolean open() {
        if (sql != null && config != null) {
            return sql.open(config.get("host"), config.get("port"), "", config.get("username"), config.get("password"));
        }
        return false;
    }

    public JSONObject initDatabase() {
        JSONObject result = new JSONObject();
        if (sql != null && config != null) {

            RandomPassword randomPassword = new RandomPassword();
            String password = randomPassword.getPassword();
            Hash hash = new Hash();

            String databaseName = config.get("database-name");
            sql.update("create database if not exists " + databaseName + " character set utf8", null);

            sql.open(config.get("host"), config.get("port"), databaseName, config.get("username"), config.get("password"));

            sql.update("create table if not exists user(uuid char(64) not null primary key, username text not null, clothes_size text, password text not null, icon text, real_name text, sex text,phone text, email text, type int not null,last_language text, state int not null) character set utf8;", null);
            sql.update("create table if not exists subject(uuid char(64) not null primary key, title text not null, content text not null, format text, official_answer text, test_case longtext, test_case_result longtext not null, answer_limit text not null, subject_image text, state int not null) character set utf8;", null);
            sql.update("create table if not exists record(uuid char(64) not null primary key, subject_id char(64) not null, user_id char(64) not null, code text, language text, result text, time text, memory text, submit_time text, state int) character set utf8;", null);
            sql.update("create table if not exists login(uuid char(64) not null primary key, user_id char(64), user_name text, user_type int,cookie text not null,ver_code char(16), host text, browser text, time long not null) character set utf8;", null);
            sql.update("create table if not exists notice(uuid char(64) not null primary key, title text not null, content text not null,user_id text not null, time text not null, priority int not null, state int not null) character set utf8;", null);
            sql.update("create table if not exists setting(uuid char(64) not null primary key, name text not null, content text not null) character set utf8;", null);
            sql.update("create table if not exists history(uuid char(64) not null primary key, user_id text, record text not null, host text, browser text, time text not null) character set utf8;", null);
            sql.update("create table if not exists file(uuid char(64) not null primary key, name text, hash text, content longtext) character set utf8;", null);
//            sql.update("",null);

            if (sql.query("select * from setting where name=?", new Object[]{"panel_color"}).size() == 0) {
                sql.update("insert into setting values(?, ?, ?)", new Object[]{UUID.randomUUID().toString(), "panel_color", "{\"white\":\"panel-default\", \"blue\":\"panel-primary\", \"green\":\"panel-success\", \"skyblue\":\"panel-info\", \"orange\":\"panel-warning\", \"red\":\"panel-danger\", \"link\":\"panel-link\"}"});
            }
            if (sql.query("select * from setting where name=?", new Object[]{"cookie"}).size() == 0) {
                sql.update("insert into setting values(?, ?, ?)", new Object[]{UUID.randomUUID().toString(), "cookie", "{\"cookieLength\":\"32\",\"timeLength\":\"1800000\"}"});
            }
            if (sql.query("select * from setting where name=?", new Object[]{"foot"}).size() == 0) {
                Encode encode = new Encode();
                sql.update("insert into setting values(?, ?, ?)", new Object[]{UUID.randomUUID().toString(), "foot", "[{\"name\":\"foot0\",\"title\":\"开发者\",\"line0\":\"桂林电子科技大学信息科技学院信息工程系\",\"line1\":\"大学生计算机创新实践基地\",\"line2\":\"tum0r\"},{\"name\":\"foot1\",\"title\":\"联系方式\",\"line0\":\"桂林壮族自治区桂林市灵川县桂林电子科技大学信息科技学院尧山校区\",\"line1\":\"541004\",\"line2\":\"\"},{\"name\":\"foot2\",\"title\":\"友情链接\",\"line0\":\"" + encode.utf8Encode("<a href=\"http://www.guit.edu.cn/\" target=\"_blank\">桂林电子科技大学信息科技学院官网</a>") + "\",\"line1\":\"" + encode.utf8Encode("<a href=\"https://720yun.com/t/2f9jeshvkf4?scene_id=19923739\" target=\"_blank\">桂林电子科技大学信息科技学院地图</a>") + "\",\"line2\":\"\"}]"});
            }
            if (sql.query("select * from setting where name=?", new Object[]{"game"}).size() == 0) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                String date = dateFormat.format(new Date().getTime());
                sql.update("insert into setting values(?, ?, ?)", new Object[]{UUID.randomUUID().toString(), "game", "{\"name\":\"信息工程系编程大赛\", \"logo\":\"\", \"sponsor\":\"信息工程系大学生计算机创新实践基地\", \"start\":\"" + date + "\", \"stop\":\"" + date + "\", \"punish\":\"00:20\",\"start_audio\":true,\"time_is_up_audio\":true,\"time_is_up\":\"00:30\",\"stop_audio\":true}"});
            }
            if (sql.query("select * from setting where name=?", new Object[]{"carousel"}).size() == 0) {
                sql.update("insert into setting values(?, ?, ?)", new Object[]{UUID.randomUUID().toString(), "carousel", "[]"});
            }
            if (sql.query("select * from setting where name=?", new Object[]{"logo"}).size() == 0) {
                sql.update("insert into setting values(?, ?, ?)", new Object[]{UUID.randomUUID().toString(), "logo", "{\"name\":\"GIITOJ\",\"logo\":\"\"}"});
            }
            if (sql.query("select * from setting where name=?", new Object[]{"register"}).size() == 0) {
                sql.update("insert into setting values(?, ?, ?)", new Object[]{UUID.randomUUID().toString(), "register", "{\"registrable\":true}"});
            }
            if (sql.query("select * from setting where name=?", new Object[]{"team"}).size() == 0) {
                sql.update("insert into setting values(?, ?, ?)", new Object[]{UUID.randomUUID().toString(), "team", "{\"mutable\":true}"});
            }

            String initUsername = "tum0r";
            if (sql.query("select * from user where username=?", new Object[]{initUsername}).size() == 0) {
                sql.update("insert into user values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{UUID.randomUUID().toString(), initUsername, "XL", hash.passwordEncryption(password), "{}", "王小狂", "男", "", "", 1, "Java", 1});
                result.put("username", initUsername);
                result.put("password", password);
            }

        }
        return result;
    }
}